package com.boot.learning.autoconfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 定义自动配置类 
 * 
 * @since JDK1.8
 * @author vencano
 * @date 2017年1月8日 上午11:24:25
 */
@Configuration //定义为配置类
@EnableConfigurationProperties(HelloProperties.class)//读入我们配置的对象类
@ConditionalOnClass(HelloService.class)//按类型查找bean,没找到跳过，不会报错；可用于不同jar包的依赖
public class HelloAutoConfiguration {

	@Autowired
	private HelloProperties helloProperties;
	
	@Bean
	@ConditionalOnMissingBean(HelloService.class)//优先查找自定义的bean,找不到自动配置新的bean
	public HelloService autoInit() {
		HelloService helloService = new HelloService();
		helloService.setMsg(helloProperties.getMsg());
		return helloService;
	}
}
